<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="add({pid:'',type:1})">添加权限</el-button>
        </div>
        <el-table :data="list" row-key="id" border>
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type === 1" type="text" @click="add({pid:row.id,type:2})">添加</el-button>
              <el-button type="text" @click="edit(row.id)">编辑</el-button>
              <el-button type="text">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 弹框 -->
    <el-dialog :visible="showDialog" :title="formData.id?'编辑':'新增'" @close="closeDialog">
      <el-form label-width="100px">
        <el-form-item label="权限名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="confirm">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, addPermission, updatePermission, getPermissionDetail } from '@/api/permission'
import transTree from '@/utils/transTree'
export default {
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        name: '',
        code: '',
        description: '',
        enVisible: 1, // 1代表启用 0代表禁用
        type: 1, // 新增的权限点类型 点击不同的新增按钮 type不一样的
        pid: '' // 要给哪一项添加子节点 id
      }
    }
  },
  mounted() {
    this.fetchList()
  },
  methods: {
    async fetchList() {
      const res = await getPermissionList()
      this.list = transTree(res.data)
    },
    // 对象参数写法可以无视参数顺序
    add({ pid, type }) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    closeDialog() {
      this.showDialog = false
      // 清空数据
      this.formData = {
        name: '',
        code: '',
        description: '',
        enVisible: 1, // 1代表启用 0代表禁用
        type: 1, // 新增的权限点类型 点击不同的新增按钮 type不一样的
        pid: '' // 要给哪一项添加子节点 id
      }
    },
    async confirm() {
      if (this.formData.id) {
        await updatePermission(this.formData)
      } else {
        await addPermission(this.formData)
      }
      // 更新列表
      this.fetchList()
      // 提示用户
      this.$message.success(`${this.formData.id ? '编辑' : '新增'}成功`)
      this.closeDialog()
    },
    async edit(id) {
      // 获取详情数据 回显
      this.showDialog = true
      const res = await getPermissionDetail(id)
      this.formData = res.data
    }
  }
}
</script>

<style lang="scss" scoped></style>
